package com.zhongen.oa.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.zhongen.common.core.util.R;
import com.zhongen.common.log.annotation.SysLog;
import com.zhongen.oa.entity.ProjectTeamEntity;
import com.zhongen.oa.entity.ProjectTeamUserRelationshipEntity;
import com.zhongen.oa.service.ProjectTeamService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目团队表
 *
 * @author Code Generator
 * @date 2024-01-11 13:59:55
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/projectTeam")
@Tag(description = "projectTeam", name = "项目团队表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ProjectTeamController {

	private final ProjectTeamService projectTeamService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param projectTeam 项目团队表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('oa_projectTeam_view')")
	public R getProjectTeamPage(@ParameterObject Page page, @ParameterObject ProjectTeamEntity projectTeam) {
		return R.ok(projectTeamService.getProjectTeamPage(page, projectTeam));
	}

	/**
	 * 通过id查询项目团队表
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('oa_projectTeam_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(projectTeamService.getById(id));
	}

	/**
	 * 新增项目团队表
	 * @param projectTeam 项目团队表
	 * @return R
	 */
	@Operation(summary = "新增项目团队表", description = "新增项目团队表")
	@SysLog("新增项目团队表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('oa_projectTeam_add')")
	public R save(@RequestBody ProjectTeamEntity projectTeam) {
		return R.ok(projectTeamService.save(projectTeam));
	}

	/**
	 * 修改项目团队表
	 * @param projectTeam 项目团队表
	 * @return R
	 */
	@Operation(summary = "修改项目团队表", description = "修改项目团队表")
	@SysLog("修改项目团队表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('oa_projectTeam_edit')")
	public R updateById(@RequestBody ProjectTeamEntity projectTeam) {
		return R.ok(projectTeamService.updateById(projectTeam));
	}

	/**
	 * 通过id解散项目团队
	 * @param relationshipEntity 项目团队人员关系
	 * @return R
	 */
	@Operation(summary = "通过id添加项目成员", description = "通过id添加项目成员")
	@PostMapping("/addTeamMembers")
	@PreAuthorize("@pms.hasPermission('oa_projectTeam_add')")
	public R addTeamMembers(@RequestBody ProjectTeamUserRelationshipEntity relationshipEntity) {
		return R.ok(projectTeamService.addTeamMembers(relationshipEntity));
	}

	/**
	 * 添加项目成员
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id解散项目团队", description = "通过id解散项目团队")
	@PostMapping("/teamBreak/{id}")
	@PreAuthorize("@pms.hasPermission('oa_projectTeam_del')")
	public R teamBreak(@PathVariable("id") Long id) {
		return R.ok(projectTeamService.teamBreakById(id));
	}

	/**
	 * 通过id删除项目团队表
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除项目团队表", description = "通过id删除项目团队表")
	@SysLog("通过id删除项目团队表")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('oa_projectTeam_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(projectTeamService.removeBatchByIds(CollUtil.toList(ids)));
	}

	/**
	 * 导出excel 表格
	 * @param projectTeam 查询条件
	 * @param ids 导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('oa_projectTeam_export')")
	public List<ProjectTeamEntity> export(ProjectTeamEntity projectTeam, Long[] ids) {
		return projectTeamService
			.list(Wrappers.lambdaQuery(projectTeam).in(ArrayUtil.isNotEmpty(ids), ProjectTeamEntity::getId, ids));
	}

}
